package com.aplink.generic.webservice;

import java.util.ArrayList;

import android.os.AsyncTask;
import android.text.TextUtils;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.constant.WebServiceContant;
import com.aplink.generic.constant.type.ActionType;
import com.aplink.generic.constant.type.ListViewSelection;
import com.aplink.generic.controller.GenericApplication;
import com.aplink.generic.database.SqlMethods;
import com.aplink.generic.network.GenericFile;
import com.aplink.generic.pojo.ContentData;
import com.aplink.generic.pojo.GenericObject;
import com.aplink.generic.pojo.MasterConfig;
import com.aplink.generic.pojo.ObjPropInfo;
import com.aplink.generic.pojo.TableInfo;
import com.aplink.generic.pojo.TableInit;
import com.aplink.generic.pojo.TableSyncData;
import com.aplink.generic.util.LogUtil;
import com.aplink.generic.util.ResourcesHelper;
import com.aplink.generic.widgets.ApLinkGallery;
import com.aplink.generic.widgets.ApLinkListView;
import com.aplink.generic.xmlparser.ContentDataParser;

public class GenericWebService {
    public static GenericWebService newInstance(
            final GenericFragment genericFragment) {
        final GenericWebService genericWebService = new GenericWebService(
                genericFragment);
        return genericWebService;
    }

    private final GenericFragment mGenericFragment;
    private TableInfo mTableInfo;
    private ActionType mTaskType;

    private final ArrayList<HttpURL> webserviceURLs;

    private GenericWebService(final GenericFragment genericFragment) {
        this.mGenericFragment = genericFragment;
        this.webserviceURLs = new ArrayList<HttpURL>();
    }

    private HttpURL baseUrl(final ContentData objPropCol,
            final GenericObject genericObject, final String table,
            final String action) {
        final MasterConfig masterConfig = GenericApplication.getInstance()
                .getMasterConfig();
        final String domain = masterConfig.getWebserviceConfig()
                .getWebserviceDomain();
        final String webserviceFunction = objPropCol.getWebserviceFunction();
        final HttpURL genericWebserviceURL = new HttpURL();
        genericWebserviceURL.setDomain(domain).setObjPropCol(objPropCol)
                .setWebserviceFunction(webserviceFunction)
                .setMethod(WebServiceContant.GET).setAction(action)
                .setTable(table);
        if (action.equals(WebServiceContant.ACTION_INSERT)) {
            genericWebserviceURL.setJsonValue(genericObject);
        } else if (action.equals(WebServiceContant.ACTION_UPDATE)) {
            final ObjPropInfo objPropInfo = objPropCol.getCreatedTime(table);
            if (objPropInfo != null) {
                final String columName = objPropCol.getCreatedTimeObject()
                        .get(0).getFullColumnName();
                final String value = genericObject.getValue(columName)
                        .toString();
                if (!TextUtils.isEmpty(value)) {
                    genericWebserviceURL.setJsonCondition(value);
                    genericWebserviceURL.setJsonValue(genericObject);
                }
            }
        } else {
            final ObjPropInfo objPropInfo = objPropCol.getCreatedTime(table);
            if (objPropInfo != null) {
                final String columName = objPropCol.getCreatedTimeObject()
                        .get(0).getFullColumnName();
                final String value = genericObject.getValue(columName)
                        .toString();
                genericWebserviceURL.setJsonCondition(value);
            }
        }
        return genericWebserviceURL;
    }

    public GenericWebService delete() {
        this.mTaskType = ActionType.DELETE;
        final ContentData objPropCol = this.mGenericFragment.getContentData();
        final GenericObject genericObject = this.mGenericFragment
                .getSelectedObject();
        final String table = objPropCol.getMainTable();
        final HttpURL httpURL = baseUrl(objPropCol, genericObject, table,
                "delete");
        this.webserviceURLs.add(httpURL);
        return this;
    }

    private GenericWebService delete(final ContentData objPropCol,
            final GenericObject genericObject, final String table) {
        final HttpURL httpURL = baseUrl(objPropCol, genericObject, table,
                WebServiceContant.ACTION_DELETE);
        this.webserviceURLs.add(httpURL);
        return this;
    }

    public GenericWebService download() {
        final MasterConfig masterConfig = GenericApplication.getInstance()
                .getMasterConfig();
        this.mTaskType = ActionType.DOWNLOAD;
        final ContentData objPropCol = this.mGenericFragment.getContentData();
        final TableSyncData tableSyncData = masterConfig.getWebserviceConfig()
                .getTableDownloads().get(objPropCol.getMainTable());
        final HttpURL httpURL = new HttpURL();
        final String webServiceDomain = masterConfig.getWebserviceConfig()
                .getWebserviceDomain();
        httpURL.setDomain(webServiceDomain).setObjPropCol(objPropCol)
                .setWebserviceFunction(objPropCol.getWebserviceFunction())
                .setTable(tableSyncData.getTableName()).setAction("load")
                .setHasDownloadFile(tableSyncData.isHavePhoto());
        if (httpURL.isHasDownloadFile()) {
            for (int i = 0; i < objPropCol.size(); i++) {
                final ObjPropInfo objPropInfo = objPropCol.valueAt(i);
                if (objPropInfo.isImageLocalPath()) {
                    httpURL.setFileObjPropInfo(objPropInfo);
                    break;
                }
            }
        }
        this.webserviceURLs.add(httpURL);
        return this;
    }

    public GenericWebService downloadAll() {
        final MasterConfig masterConfig = GenericApplication.getInstance()
                .getMasterConfig();
        this.mTaskType = ActionType.DOWNLOAD_ALL;
        for (int i = 0; i < masterConfig.getWebserviceConfig()
                .getTableDownloads().size(); i++) {
            final TableSyncData tableSyncData = masterConfig
                    .getWebserviceConfig().getTableDownloads().valueAt(i);
            final int contentDataFileId = tableSyncData
                    .getWebserviceFunctionID();
            final ContentData objPropCol = ContentDataParser.getInstance()
                    .parseIntoObjProp(contentDataFileId);

            final HttpURL httpURL = new HttpURL();
            final String webServiceDomain = masterConfig.getWebserviceConfig()
                    .getWebserviceDomain();
            httpURL.setDomain(webServiceDomain).setObjPropCol(objPropCol)
                    .setWebserviceFunction(objPropCol.getWebserviceFunction())
                    .setTable(tableSyncData.getTableName()).setAction("load")
                    .setHasDownloadFile(tableSyncData.isHavePhoto());
            if (httpURL.isHasDownloadFile()) {
                for (int j = 0; j < objPropCol.size(); j++) {
                    final ObjPropInfo objPropInfo = objPropCol.valueAt(j);
                    if (objPropInfo.isImageLocalPath()) {
                        httpURL.setFileObjPropInfo(objPropInfo);
                        break;
                    }
                }
            }
            this.webserviceURLs.add(httpURL);
        }
        return this;
    }

    public void excute() {
        final HttpURL[] urls = this.webserviceURLs
                .toArray(new HttpURL[this.webserviceURLs.size()]);
        new HttpTask(this.mGenericFragment, this.mTaskType).executeOnExecutor(
                AsyncTask.THREAD_POOL_EXECUTOR, urls);
    }

    public GenericWebService forceUpload() {
        this.mTaskType = ActionType.UPLOAD;
        final MasterConfig masterConfig = GenericApplication.getInstance()
                .getMasterConfig();
        for (int i = 0; i < masterConfig.getWebserviceConfig()
                .getTableDownloads().size(); i++) {
            final TableSyncData tableSyncData = masterConfig
                    .getWebserviceConfig().getTableDownloads().valueAt(i);
            final int contentDataFileId = tableSyncData
                    .getWebserviceFunctionID();
            final ContentData objPropCol = ContentDataParser.getInstance()
                    .parseIntoObjProp(contentDataFileId);
            final ArrayList<GenericObject> distinctObjectList = this.mGenericFragment
                    .getDatabaseGet().selectDistinct(
                            tableSyncData.getTableName(), objPropCol, null);
            for (final GenericObject object : distinctObjectList) {
                this.delete(objPropCol, object, tableSyncData.getTableName());
                this.upload(object, objPropCol, tableSyncData.isHavePhoto());
            }
        }
        return this;
    }

    public GenericWebService forgotPassword() {
        this.mTaskType = ActionType.FORGOT_PASSWORD;
        final GenericObject genericObject = this.mGenericFragment
                .getSelectedObject();
        final ContentData objPropCol = this.mGenericFragment.getContentData();
        final MasterConfig masterConfig = GenericApplication.getInstance()
                .getMasterConfig();
        final String domain = masterConfig.getWebserviceConfig()
                .getWebserviceDomain();
        final HttpURL httpURL = new HttpURL();
        httpURL.setDomain(domain)
                .setWebserviceFunction(
                        masterConfig.getWebserviceConfig().getForgotPassword())
                .setObjPropCol(objPropCol).setEmail(genericObject)
                .setMethod(WebServiceContant.GET);
        this.webserviceURLs.add(httpURL);
        return this;
    }

    public GenericWebService initDataTable() {
        this.mTaskType = ActionType.INIT_DATA;
        final MasterConfig masterConfig = GenericApplication.getInstance()
                .getMasterConfig();

        for (int i = 0; i < masterConfig.getWebserviceConfig()
                .initTableNameInits().size(); i++) {
            final TableInit tableInit = masterConfig.getWebserviceConfig()
                    .initTableNameInits().valueAt(i);
            final HttpURL url = new HttpURL();
            url.setDomain(
                    masterConfig.getWebserviceConfig().getWebserviceDomain())
                    .setWebserviceFunction(
                            masterConfig.getWebserviceConfig().getInitData())
                    .setTable(tableInit.initTableNameName());
            this.webserviceURLs.add(url);
        }

        for (int i = 0; i < masterConfig.getWebserviceConfig()
                .getTableDownloads().size(); i++) {
            final TableSyncData tableSyncData = masterConfig
                    .getWebserviceConfig().getTableDownloads().valueAt(i);

            final int contentDataFileId = ResourcesHelper
                    .getRawIdByName(tableSyncData.getWebserviceFunction());
            if (contentDataFileId != ResourcesHelper.RESOURCE_NOT_FOUND) {
                final ContentData objPropCol = ContentDataParser.getInstance()
                        .parseIntoObjProp(contentDataFileId);
                final HttpURL httpURL = new HttpURL();
                final String webServiceDomain = masterConfig
                        .getWebserviceConfig().getWebserviceDomain();
                httpURL.setDomain(webServiceDomain)
                        .setObjPropCol(objPropCol)
                        .setWebserviceFunction(
                                objPropCol.getWebserviceFunction())
                        .setTable(tableSyncData.getTableName())
                        .setAction("load")
                        .setHasDownloadFile(tableSyncData.isHavePhoto());
                if (httpURL.isHasDownloadFile()) {
                    if (objPropCol.getImageLocalPath() != null) {
                        httpURL.setFileObjPropInfo(objPropCol
                                .getImageLocalPath());
                    }
                }
                this.webserviceURLs.add(httpURL);
            }
        }
        return this;
    }

    public TableInfo initTableNameInfo() {
        return this.mTableInfo;
    }

    public GenericWebService insert() {
        this.mTaskType = ActionType.INSERT;
        HttpURL httpURL = null;
        final ContentData objPropCol = this.mGenericFragment.getContentData();
        final GenericObject genericObject = this.mGenericFragment
                .getSelectedObject();
        final String table = objPropCol.getMainTable();
        if (this.mGenericFragment.getFormDesign().isHasGallery()) {
            final ApLinkGallery gallery = (ApLinkGallery) this.mGenericFragment
                    .getAPLinkViewByID(this.mGenericFragment.getFormDesign()
                            .getGalleryID());
            final MasterConfig masterConfig = GenericApplication.getInstance()
                    .getMasterConfig();
            final String domain = masterConfig.getWebserviceConfig()
                    .getWebserviceDomain();
            httpURL = new HttpURL();
            httpURL.setObjPropCol(objPropCol)
                    .setDomain(domain)
                    .setWebserviceFunction(
                            objPropCol.getWebserviceFunction().replace(
                                    "DataHandle?", "ImageDataHandle?"))
                    .setTable(table).setAction("insert")
                    .setJsonValue(genericObject)
                    .setMethod(WebServiceContant.POST)
                    .setListUploadFiles(gallery.getInsertFiles());
        } else {
            httpURL = baseUrl(objPropCol, genericObject, table,
                    WebServiceContant.ACTION_INSERT);
        }
        this.webserviceURLs.add(httpURL);
        return this;
    }

    public GenericWebService login() {
        this.mTaskType = ActionType.LOGIN;
        final ContentData objPropCol = this.mGenericFragment.getContentData();
        final GenericObject genericObject = this.mGenericFragment
                .getBindingObject();
        final MasterConfig masterConfig = GenericApplication.getInstance()
                .getMasterConfig();
        final String domain = masterConfig.getWebserviceConfig()
                .getWebserviceDomain();
        final String webserviceFunction = objPropCol.getWebserviceFunction();
        final HttpURL httpURL = new HttpURL();
        httpURL.setObjPropCol(objPropCol).setDomain(domain)
                .setWebserviceFunction(webserviceFunction)
                .setJsonValue(genericObject);
        this.webserviceURLs.add(httpURL);
        return this;
    }

    public GenericWebService save() {
        this.mTaskType = ActionType.SAVE;
        final ContentData objPropCol = this.mGenericFragment.getContentData();
        final GenericObject genericObject = this.mGenericFragment
                .getSelectedObject();
        final String table = objPropCol.getMainTable();
        final ObjPropInfo primaryKey = objPropCol.getPrimaryKey(table);
        boolean isUpdate = false;
        if (TextUtils.isEmpty(genericObject.getValue(primaryKey
                .getFullColumnName()))) {
            isUpdate = false;
        } else {
            isUpdate = this.mGenericFragment.getDatabaseGet().isExists(
                    genericObject, objPropCol);
        }
        HttpURL httpURL = null;
        if (isUpdate) {
            if (this.mGenericFragment.getFormDesign().isHasGallery()) {
                final ApLinkGallery gallery = (ApLinkGallery) this.mGenericFragment
                        .getAPLinkViewByID(this.mGenericFragment
                                .getFormDesign().getGalleryID());
                final MasterConfig masterConfig = GenericApplication
                        .getInstance().getMasterConfig();
                final String domain = masterConfig.getWebserviceConfig()
                        .getWebserviceDomain();
                httpURL = new HttpURL();
                httpURL.setObjPropCol(objPropCol)
                        .setDomain(domain)
                        .setWebserviceFunction(
                                objPropCol.getWebserviceFunction().replace(
                                        "DataHandle?", "ImageDataHandle?"))
                        .setTable(table).setAction("update")
                        .setJsonValue(genericObject)
                        .setMethod(WebServiceContant.POST)
                        .setListUploadFiles(gallery.getInsertFiles())
                        .setListDeleteFiles(gallery.getDeleteFiles());
                final ObjPropInfo objPropInfo = objPropCol
                        .getCreatedTime(table);
                if (objPropInfo != null) {
                    final String columName = objPropCol.getCreatedTimeObject()
                            .get(0).getFullColumnName();
                    final String value = genericObject.getValue(columName)
                            .toString();
                    httpURL.setJsonCondition(value);
                }
                this.webserviceURLs.add(httpURL);
            } else if (this.mGenericFragment.getFormDesign().isHasListView()) {
                final ApLinkListView apLinkListView = (ApLinkListView) this.mGenericFragment
                        .getAPLinkViewByID(this.mGenericFragment
                                .getFormDesign().getListViewID());
                if (apLinkListView.getListViewSelection() != null) {
                    if (apLinkListView.getListViewSelection().equals(
                            ListViewSelection.MULTIPLE_SELECTION)) {
                        for (final GenericObject object : apLinkListView
                                .getDeleteList()) {
                            final HttpURL url = baseUrl(objPropCol, object,
                                    table, WebServiceContant.ACTION_DELETE);
                            this.webserviceURLs.add(url);
                        }

                        for (final GenericObject object : apLinkListView
                                .getInsertList()) {
                            final HttpURL url = baseUrl(objPropCol, object,
                                    table, WebServiceContant.ACTION_INSERT);
                            this.webserviceURLs.add(url);
                        }
                    } else if (apLinkListView.getListViewSelection().equals(
                            ListViewSelection.SINGLE_SELECTION)) {
                        for (final GenericObject object : apLinkListView
                                .getInsertList()) {
                            final HttpURL url = baseUrl(objPropCol, object,
                                    table, WebServiceContant.ACTION_UPDATE);
                            this.webserviceURLs.add(url);
                        }
                    }
                }

            } else {
                httpURL = baseUrl(objPropCol, genericObject, table,
                        WebServiceContant.ACTION_UPDATE);
                this.webserviceURLs.add(httpURL);
            }
        } else {
            if (this.mGenericFragment.getFormDesign().isHasGallery()) {
                final ApLinkGallery gallery = (ApLinkGallery) this.mGenericFragment
                        .getAPLinkViewByID(this.mGenericFragment
                                .getFormDesign().getGalleryID());
                final MasterConfig masterConfig = GenericApplication
                        .getInstance().getMasterConfig();
                final String domain = masterConfig.getWebserviceConfig()
                        .getWebserviceDomain();
                httpURL = new HttpURL();
                httpURL.setObjPropCol(objPropCol)
                        .setDomain(domain)
                        .setWebserviceFunction(
                                objPropCol.getWebserviceFunction().replace(
                                        "DataHandle?", "ImageDataHandle?"))
                        .setTable(table).setAction("insert")
                        .setJsonValue(genericObject)
                        .setMethod(WebServiceContant.POST)
                        .setListUploadFiles(gallery.getInsertFiles());
                this.webserviceURLs.add(httpURL);
            } else if (this.mGenericFragment.getFormDesign().isHasListView()) {
                final ApLinkListView apLinkListView = (ApLinkListView) this.mGenericFragment
                        .getAPLinkViewByID(this.mGenericFragment
                                .getFormDesign().getListViewID());
                for (final GenericObject object : apLinkListView
                        .getInsertList()) {
                    final HttpURL url = baseUrl(objPropCol, object, table,
                            WebServiceContant.ACTION_INSERT);
                    this.webserviceURLs.add(url);
                }
            } else {
                httpURL = baseUrl(objPropCol, genericObject, table,
                        WebServiceContant.ACTION_INSERT);
                this.webserviceURLs.add(httpURL);
            }
        }
        return this;
    }

    public void setTableInfo(final TableInfo tableInfo) {
        this.mTableInfo = tableInfo;
    }

    public GenericWebService update() {
        this.mTaskType = ActionType.UPDATE;
        final ContentData objPropCol = this.mGenericFragment.getContentData();
        final GenericObject genericObject = this.mGenericFragment
                .getBindingObject();
        final String table = objPropCol.getMainTable();
        HttpURL httpURL = null;
        if (this.mGenericFragment.getFormDesign().isHasGallery()) {
            final ApLinkGallery gallery = (ApLinkGallery) this.mGenericFragment
                    .getAPLinkViewByID(this.mGenericFragment.getFormDesign()
                            .getGalleryID());
            final MasterConfig masterConfig = GenericApplication.getInstance()
                    .getMasterConfig();
            final String domain = masterConfig.getWebserviceConfig()
                    .getWebserviceDomain();
            httpURL = new HttpURL();
            httpURL.setObjPropCol(objPropCol)
                    .setDomain(domain)
                    .setWebserviceFunction(
                            objPropCol.getWebserviceFunction().replace(
                                    "DataHandle?", "ImageDataHandle?"))
                    .setTable(table).setAction("update")
                    .setJsonValue(genericObject)
                    .setMethod(WebServiceContant.POST)
                    .setListUploadFiles(gallery.getInsertFiles())
                    .setListDeleteFiles(gallery.getDeleteFiles());
            final ObjPropInfo objPropInfo = objPropCol.getCreatedTime(table);
            if (objPropInfo != null) {
                final String columName = objPropCol.getCreatedTimeObject()
                        .get(0).getFullColumnName();
                final String value = genericObject.getValue(columName)
                        .toString();
                httpURL.setJsonCondition(value);
            }
        } else {
            httpURL = baseUrl(objPropCol, genericObject, table,
                    WebServiceContant.ACTION_UPDATE);
        }
        this.webserviceURLs.add(httpURL);
        return this;
    }

    public GenericWebService upload() {
        this.mTaskType = ActionType.UPLOAD;

        final MasterConfig masterConfig = GenericApplication.getInstance()
                .getMasterConfig();
        for (int i = 0; i < masterConfig.getWebserviceConfig()
                .getTableDownloads().size(); i++) {
            final TableSyncData tableSyncData = masterConfig
                    .getWebserviceConfig().getTableDownloads().valueAt(i);
            if (tableSyncData.isDownloadOnly()) {
                continue;
            }
            final String mainTableName = tableSyncData.getTableName();

            final int contentDataFileId = tableSyncData
                    .getWebserviceFunctionID();
            final ContentData objPropCol = ContentDataParser.getInstance()
                    .parseIntoObjProp(contentDataFileId);
            final String syncStatusColumn = objPropCol.getSyncStatus(
                    tableSyncData.getTableName()).getFullColumnName();

            final SqlMethods sqlMethods = SqlMethods.getInstance().isNotNull(
                    syncStatusColumn);

            if (tableSyncData.isHavePhoto()) {
                final ObjPropInfo objPrimaryKey = objPropCol
                        .getPrimaryKey(mainTableName);
                final ObjPropInfo imageLocalPath = objPropCol
                        .getImageLocalPath();

                final String distinctColumn = objPrimaryKey.getFullColumnName();
                final ArrayList<GenericObject> distinctObjectList = this.mGenericFragment
                        .getDatabaseGet().selectDistinct(
                                tableSyncData.getTableName(), objPropCol,
                                sqlMethods.toString());

                for (final GenericObject distinctObject : distinctObjectList) {
                    final String action = distinctObject.getValue(
                            syncStatusColumn).toString();
                    final HttpURL httpURL = new HttpURL();
                    httpURL.setObjPropCol(objPropCol)
                            .setDomain(
                                    masterConfig.getWebserviceConfig()
                                            .getWebserviceDomain())
                            .setTable(tableSyncData.getTableName())
                            .setAction(action).setJsonValue(distinctObject);

                    if (!objPropCol.getCreatedTimeObject().isEmpty()) {
                        if (!action.equalsIgnoreCase("insert")) {
                            final String columName = objPropCol.getCreatedTime(
                                    mainTableName).getFullColumnName();
                            final String value = distinctObject.getValue(
                                    columName).toString();
                            httpURL.setJsonCondition(value);
                        }
                    } else {
                        LogUtil.error(LogUtil.TAG_WEBSERVICE,
                                "Created time is required for upload",
                                "Upload", getClass());
                        throw new NullPointerException(
                                "Created time is required for upload");
                    }

                    if (action.equalsIgnoreCase("delete")) {
                        httpURL.setWebserviceFunction(objPropCol
                                .getWebserviceFunction().replace(
                                        "ImageDataHandle?", "DataHandle?"));
                        httpURL.setMethod(WebServiceContant.GET);
                    } else {
                        httpURL.setWebserviceFunction(objPropCol
                                .getWebserviceFunction().replace("DataHandle?",
                                        "ImageDataHandle?"));

                        final String value = distinctObject.getValue(
                                distinctColumn).toString();

                        final SqlMethods whereClause = SqlMethods
                                .getInstance()
                                .equal(objPrimaryKey.getFullColumnName(), value);

                        final ArrayList<GenericFile> listUploadFiles = new ArrayList<GenericFile>();
                        final ArrayList<GenericFile> listDeleteFile = new ArrayList<GenericFile>();

                        final ObjPropInfo fileSyncStatus = objPropCol
                                .getSyncStatus(imageLocalPath.getTable());

                        whereClause.and().isNotNull(
                                fileSyncStatus.getFullColumnName());
                        final ArrayList<GenericObject> images = this.mGenericFragment
                                .getDatabaseGet().getList(objPropCol,
                                        whereClause.toString());

                        for (final GenericObject image : images) {
                            final String syncStatus = image.getValue(
                                    fileSyncStatus.getFullColumnName())
                                    .toString();

                            final String localPath = image.getValue(
                                    imageLocalPath.getFullColumnName())
                                    .toString();
                            final GenericFile file = new GenericFile();
                            file.setLocalPath(localPath);
                            file.setFileNameByURl(localPath);
                            if (syncStatus.equalsIgnoreCase("insert")) {
                                listUploadFiles.add(file);
                            } else {
                                listDeleteFile.add(file);
                            }
                        }

                        httpURL.setListUploadFiles(listUploadFiles);
                        httpURL.setListDeleteFiles(listDeleteFile);
                        httpURL.setMethod(WebServiceContant.POST);
                    }
                    this.webserviceURLs.add(httpURL);
                }
            } else {
                final ArrayList<GenericObject> genericObjects = this.mGenericFragment
                        .getDatabaseGet().getList(objPropCol,
                                sqlMethods.toString());
                for (final GenericObject obj : genericObjects) {
                    final String action = obj.getValue(syncStatusColumn)
                            .toString();
                    final String domain = masterConfig.getWebserviceConfig()
                            .getWebserviceDomain();
                    final String webserviceFunction = objPropCol
                            .getWebserviceFunction();
                    final HttpURL httpURL = new HttpURL();
                    httpURL.setObjPropCol(objPropCol).setDomain(domain)
                            .setWebserviceFunction(webserviceFunction)
                            .setTable(tableSyncData.getTableName())
                            .setAction(action).setJsonValue(obj);
                    if (!objPropCol.getCreatedTimeObject().isEmpty()) {
                        if (!action.equalsIgnoreCase("insert")) {
                            final String columName = objPropCol
                                    .getCreatedTimeObject().get(0)
                                    .getFullColumnName();
                            final String value = obj.getValue(columName)
                                    .toString();
                            httpURL.setJsonCondition(value);
                        }
                    }
                    httpURL.setMethod(WebServiceContant.GET);
                    this.webserviceURLs.add(httpURL);
                }
            }
        }
        return this;
    }

    private void upload(final GenericObject distinctObject,
            final ContentData objPropCol, final boolean isHavePhoto) {
        final String mainTableName = objPropCol.getMainTable();
        final ObjPropInfo objPrimaryKey = objPropCol
                .getPrimaryKey(mainTableName);
        final ObjPropInfo imageLocalPath = objPropCol.getImageLocalPath();
        final String distinctColumn = objPrimaryKey.getFullColumnName();
        final MasterConfig masterConfig = GenericApplication.getInstance()
                .getMasterConfig();
        final String syncStatusColumn = objPropCol.getSyncStatus(mainTableName)
                .getFullColumnName();
        if (isHavePhoto) {
            final String action = distinctObject.getValue(syncStatusColumn)
                    .toString();
            final HttpURL httpURL = new HttpURL();
            httpURL.setObjPropCol(objPropCol)
                    .setDomain(
                            masterConfig.getWebserviceConfig()
                                    .getWebserviceDomain())
                    .setTable(mainTableName).setAction(action)
                    .setJsonValue(distinctObject);
            if (!objPropCol.getCreatedTimeObject().isEmpty()) {
                if (!action.equalsIgnoreCase("insert")) {
                    final String columName = objPropCol.getCreatedTime(
                            mainTableName).getFullColumnName();
                    final String value = distinctObject.getValue(columName)
                            .toString();
                    httpURL.setJsonCondition(value);
                }
            } else {
                LogUtil.error(LogUtil.TAG_WEBSERVICE,
                        "Created time is required for upload", "Upload",
                        getClass());
                throw new NullPointerException(
                        "Created time is required for upload");
            }

            if (action.equalsIgnoreCase("delete")) {
                httpURL.setWebserviceFunction(objPropCol
                        .getWebserviceFunction().replace("ImageDataHandle?",
                                "DataHandle?"));
                httpURL.setMethod(WebServiceContant.GET);
            } else {
                httpURL.setWebserviceFunction(objPropCol
                        .getWebserviceFunction().replace("DataHandle?",
                                "ImageDataHandle?"));

                final String value = distinctObject.getValue(distinctColumn)
                        .toString();

                final SqlMethods whereClause = SqlMethods.getInstance().equal(
                        objPrimaryKey.getFullColumnName(), value);

                final ArrayList<GenericFile> listUploadFiles = new ArrayList<GenericFile>();
                final ArrayList<GenericFile> listDeleteFile = new ArrayList<GenericFile>();

                final ObjPropInfo fileSyncStatus = objPropCol
                        .getSyncStatus(imageLocalPath.getTable());

                whereClause.and().isNotNull(fileSyncStatus.getFullColumnName());
                final ArrayList<GenericObject> images = this.mGenericFragment
                        .getDatabaseGet().getList(objPropCol,
                                whereClause.toString());

                for (final GenericObject image : images) {
                    final String syncStatus = image.getValue(
                            fileSyncStatus.getFullColumnName()).toString();

                    final String localPath = image.getValue(
                            imageLocalPath.getFullColumnName()).toString();
                    final GenericFile file = new GenericFile();
                    file.setLocalPath(localPath);
                    file.setFileNameByURl(localPath);
                    if (syncStatus.equalsIgnoreCase("insert")) {
                        listUploadFiles.add(file);
                    } else {
                        listDeleteFile.add(file);
                    }
                }

                httpURL.setListUploadFiles(listUploadFiles);
                httpURL.setListDeleteFiles(listDeleteFile);
                httpURL.setMethod(WebServiceContant.POST);
            }
            this.webserviceURLs.add(httpURL);
        } else {
            final String action = distinctObject.getValue(syncStatusColumn)
                    .toString();
            final String domain = masterConfig.getWebserviceConfig()
                    .getWebserviceDomain();
            final String webserviceFunction = objPropCol
                    .getWebserviceFunction();
            final HttpURL httpURL = new HttpURL();
            httpURL.setObjPropCol(objPropCol).setDomain(domain)
                    .setWebserviceFunction(webserviceFunction)
                    .setTable(mainTableName).setAction(action)
                    .setJsonValue(distinctObject);
            if (!objPropCol.getCreatedTimeObject().isEmpty()) {
                if (!action.equalsIgnoreCase("insert")) {
                    final String columName = objPropCol.getCreatedTimeObject()
                            .get(0).getFullColumnName();
                    final String value = distinctObject.getValue(columName)
                            .toString();
                    httpURL.setJsonCondition(value);
                }
            }
            httpURL.setMethod(WebServiceContant.GET);
            this.webserviceURLs.add(httpURL);
        }
    }
}
